package com.atguigu.eduservice.controller;


import com.atguigu.commonutil.CourseWebVoOrder;
import com.atguigu.commonutil.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.vo.CourseQueryVo;
import com.atguigu.eduservice.vo.frontVo.CourseWebVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-02-27
 */
@RestController
@RequestMapping("/eduservice/edu-course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;



    //添加课程基本信息方法
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    @GetMapping("/getByCourseId/{courseId}")
    public R getByCourseId(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfoByCourseId(courseId);
        return R.ok().data("courseInfo", courseInfoVo);
    }

    @PostMapping("updateCourseInfo")
    public R updateByCourseId(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @GetMapping("/getpublishCourseInfo/{courseId}")
    public R getpublishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo coursePublishVo = eduCourseService.getpublishCourseInfo(courseId);
        return R.ok().data("coursePublish", coursePublishVo);
    }

    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态
        boolean b = eduCourseService.updateById(eduCourse);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("/pageCourseQuery/{page}/{limit}")
    public R pageQuery(@PathVariable Long page,
                       @PathVariable Long limit,
                       @RequestBody(required = false)CourseQueryVo courseQueryVo) {
        Page<EduCourse> pageParam = new Page<>(page, limit);
        eduCourseService.pageQuery(pageParam, courseQueryVo);
        List<EduCourse> list = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("list", list).data("total", total);
    }

    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        boolean flag = eduCourseService.deleteCourse(courseId);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}

